#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>

using namespace std;
const int N = 1e5 + 10;

int n;
struct Query{
    int i, j, e;
}query[N];
int p[N];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

void solve(){
    map<int, int> mp;
    int cnt = 1;

    scanf("%d", &n);

    for(int i = 1; i <= n; i ++) p[i] = i;

    for(int i = 0; i < n; i ++){
        int a, b, e;
        scanf("%d%d%d", &a, &b, &e);
        if(!mp.count(a))mp[a] = cnt ++;
        if(!mp.count(b))mp[b] = cnt ++;
        a = mp[a], b = mp[b];
        if(e == 1){
            int pa = find(a), pb = find(b);
            if(pa != pb){
                p[pa] = pb;
            }
        }
        query[i].i = a, query[i].j = b, query[i].e = e;
    }
    bool flag = true;
    for(int i = 0; i < n; i ++){
        if(!query[i].e){
            int a = find(query[i].i), b = find(query[i].j);
            if(a == b){
                flag = false;
                break;
            }
        }
    }

    if(flag){
        printf("YES\n");
    }else{
        printf("NO\n");
    }


}


int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }
    return 0;
}